package com.turman.composebook.views

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.turman.composebook.R
import com.turman.composebook.ui.theme.backgroundColor

@Composable
fun LazyVerticalGridSampleView(modifier: Modifier=Modifier){
    LazyVerticalGrid(
        contentPadding = PaddingValues(12.dp),
        modifier = modifier
            .fillMaxSize()
            .background(backgroundColor),
        columns = GridCells.Fixed(4),
        content = {
            items(snacks.size) { it ->
                GridSnackCard(snack = snacks[it])
            }
        }
    )
}

@Composable
fun GridSnackCard(
    modifier: Modifier = Modifier,
    snack: Snack,
) {
    Box(contentAlignment = Alignment.TopEnd, modifier = modifier.padding(4.dp)) {


        Image(
            contentScale = ContentScale.FillBounds,
            modifier = modifier
                .size(120.dp)
                .clip(RoundedCornerShape(8.dp))
                .clickable { },
            painter = rememberAsyncImagePainter(
                ImageRequest.Builder(LocalContext.current).data(data = snack.imageUrl)
                    .apply(block = fun ImageRequest.Builder.() {
                        placeholder(drawableResId = R.drawable.placeholder)
                    }).build()
            ),
            contentDescription = null
        )
    }
}